package com.example.admin.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.BlogMapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.BlogService;
import com.example.xo.common.pojo.Blog;
import com.example.xo.common.pojo.Category;
import com.example.xo.vo.BlogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liguoping
 */
@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Blog blog) {
        //雪花id
        String id = IdUtil.getSnowflakeNextIdStr();
        //判断是否重复
        Blog blogInDb = findBlogByTitle(blog.getTitle());
        if (blogInDb != null) {
            return false;
        }
        //判断是否写入成功
        return blogMapper.insert(blog) == 1;
    }

    @Override
    public boolean deleteById(String id) {
        Blog blog = findBlogId(id);
        if(blog==null){
            return false;
        }
        blog.setDeleted(LocalDateTime.now());

        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, id);
        int res = blogMapper.update(blog, wrapper);
        return res == 1;
    }

    @Override
    public boolean update(Blog blog) {
        Blog blogidb = findBlogId(blog.getBlogId());
        blog.setUpdated(LocalDateTime.now());

        if (!blogidb.getAuthor().equals(blog.getAuthor())){
            return false;
        }
        log.info("{}",blogidb);
        BlogVO blogIdDb = findByTitle(blog.getTitle());
        if (blogIdDb != null &&! blog.getBlogId().equals(blogIdDb.getBlogId())) {
            return false;
        }

        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, blog.getBlogId());
        wrapper.isNull(Blog::getDeleted);
        int res = blogMapper.update(blog, wrapper);
        log.info("res:{}",res);
        return res>0;
    }

    @Override
    public List<BlogVO> findAll() {
        List<BlogVO> vos = new ArrayList<>();
        //查询blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.orderByDesc(Blog::getCreated);
        List<Blog> blogs = blogMapper.selectList(wrapper);
        //根据blog的categoryId来获取分类名
        for (Blog blog : blogs) {
            //通过分类id获取分类对象
            //todo 这段代码可以提取为一个方法
            Category category = categoryMapper.selectById(blog.getCategoryId());
            //组装vo
            BlogVO vo = new BlogVO();
            vo.setBlogId(blog.getBlogId());
            vo.setAuthor(blog.getAuthor());
            vo.setTitle(blog.getTitle());
            vo.setCategoryName(category.getCategoryName());
            vo.setCreated(blog.getCreated());
            //加入到容器中
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public BlogVO findById(String id) {
        //查找blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId, id);
        Blog blog = blogMapper.selectOne(wrapper);
        //加强
        if (blog == null) {
            return null;
        }
        //通过blog中的categoryId查找
        Category category = categoryMapper.selectById(blog.getCategoryId());
        //组装vo
        BlogVO vo = new BlogVO();
        vo.setBlogId(blog.getBlogId());
        vo.setAuthor(blog.getAuthor());
        vo.setTitle(blog.getTitle());
        vo.setCategoryName(category.getCategoryName());
        vo.setCreated(blog.getCreated());

        return vo;
    }

    @Override
    public Blog findBlogId(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, id);
        wrapper.isNull(Blog::getDeleted);
        return blogMapper.selectOne(wrapper);
    }

    @Override
    public BlogVO findByTitle(String title) {
        //查询blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle, title);
        Blog blog = blogMapper.selectOne(wrapper);

        //加强
        if (blog == null) {
            return null;
        }

        Category category = categoryMapper.selectById(blog.getCategoryId());

        //封装vo
        BlogVO vo = new BlogVO();
        vo.setBlogId(blog.getBlogId());
        vo.setAuthor(blog.getAuthor());
        vo.setCategoryName(category.getCategoryName());
        vo.setTitle(blog.getTitle());
        vo.setCreated(blog.getCreated());

        return vo;

    }

    @Override
    public Blog findPoById(String id) {
        LambdaQueryWrapper<Blog> wrapper=new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        return blogMapper.selectOne(wrapper);
    }
    public Blog findBlogByTitle(String title) {
        //查找blog
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle, title);
        Blog blog = blogMapper.selectOne(wrapper);

        return blog;
    }
}


